#!/bin/sh
# COPYRIGHT lipujun
# file name: monitor.sh
# function: This script is used to monitor
#
# author: lipujun

#####################!/bin/bash

source /AIR_FLASH/script/air.conf
source /AIR_FLASH/script/shcommon
source /AIR_FLASH/system/air_sys.conf

time_create=0
app_time=0
usbsh_time=0
pingsh_time=0
clearTmp_time=0
linuxName=`uname -r`

while [ 1 ]
do
    sleep 1
    time_create=$[$time_create+1]
    app_time=$[$app_time+1]
    usbsh_time=$[$usbsh_time+1]
    pingsh_time=$[$pingsh_time+1]
    clearTmp_time=$[$clearTmp_time+1]

    if [ $app_time -ge $check_app_max_interval ] ; then
        procID=`pgrep $APP_NAME`
        if [ "" == "$procID" ]; then
            Error "The $APP_NAME do not exist and restart it"

            cd ${prefix}/script
	    ./boot.sh &
        else
           appNum=`ps -eLf | grep $APP_NAME | grep -v grep | wc -l`
           if [[ ${appNum} ]] && [[ ${appNum} -lt $appThreadNum ]] ; then
               Error "The $APP_NAME thread error and restart it"
               killall -9 $APP_NAME
	       $prefix/$APP_NAME -c $prefix/script/air.conf &
           fi
        fi
        app_time=0
    fi
    if [ ${dataCollect_usb} -gt 0 ] ; then
        if [ $usbsh_time -ge $check_usbsh_max_interval ] ; then
            procID=`pgrep usb.sh`
            if [ "" == "$procID" ]; then
	        Error "The usb.sh do not exist and restart it"
                killall -9 rsync > /dev/null 2>&1
	        cd ${prefix}/script/
	        ./usb.sh &
            else
               appNum=`ps x | grep usb.sh | grep -v grep | wc -l`
               if [[ ${appNum} ]] && [[ ${appNum} -lt $usbThreadNum ]] ; then
                   Error "The usb.sh thread error and restart it"
                   killall -9 rsync > /dev/null 2>&1
                   killall -9 usb.sh
                   cd ${prefix}/script
	          ./usb.sh &
               fi
            fi
            koerr=`lsmod | grep "\-"| wc -l`
            if [ $koerr -gt 0 ] ; then
                reboot
            fi
	    usbsh_time=0
        fi
    fi
    if [[ -f "${prefix}/script/bondCheck.sh" ]] && [[ "${Netbond}" == "bond0" ]] ; then
        if [ $pingsh_time -ge $check_pingsh_max_interval ] ; then
            procID=`pgrep bondCheck.sh`
            if [ "" == "$procID" ]; then
	        Error "The bondCheck.sh do not exist and restart it"
	        cd ${prefix}/script/
	        ./bondCheck.sh &
            fi
            pingsh_time=0
        fi
    fi
    if [ $clearTmp_time -gt $tmpfs_rm_interval ] ; then
        if [ "${linuxName}" != "4.19.68-dirty" ] ; then
             rm -fr /var/log/*.gz
        else
	    echo "" > /var/log/debug
	    echo "" > /var/log/syslog
	    Info "clear /var/log/debug and /var/log/syslog"
	    clearTmp_time=0

	    rSize=$(df | grep /var/volatile | awk '{print int($5)}')
	    if [ $rSize -gt $tmpfs_rm_limit_usedRatio ] ; then
		echo "" > /var/log/kern.log
		Info "/var/volatile tmpfs > 70% clear /var/log/kern.log"
	    fi
        fi
    fi    
done &

